

#include "iic.h"



void IIC_Init(){

     IIC_GPIO_Init();




}

void IIC_GPIO_Init(){

	 GPIO_InitTypeDef GPIO_Struct;
	
	  IIC_PORT_ENABLE;
	
	  GPIO_Struct.Pin=SDA | SCL;
	  GPIO_Struct.Mode=GPIO_MODE_OUTPUT_PP;
	  GPIO_Struct.Speed=GPIO_SPEED_HIGH;
	
	  HAL_GPIO_Init(IIC_PORT,&GPIO_Struct);
	  
	  SCL_H;
	  SDA_H;
	

}


void IIC_WrCmd(uint8_t cmd){


	 IIC_Start();
	 IIC_WrByte(Address);            //Slave address,SA0=0
	 IIC_Ack();	
	 IIC_WrByte(0x00);			//write command
	 IIC_Ack();	
	 IIC_WrByte(cmd); 
	 IIC_Ack();	
	 IIC_Stop();
	 


}


void IIC_WrData(uint8_t data){


   IIC_Start();
	 IIC_WrByte(Address);            //Slave address,SA0=0
	 IIC_Ack();	
	 IIC_WrByte(0x40);			//write command
	 IIC_Ack();	
	 IIC_WrByte(data); 
	 IIC_Ack();	
	 IIC_Stop();




}



void IIC_WrByte(uint8_t data){


     uint8_t i=0,flag=0x80;
	
	   SCL_L;
	
	  for(i=0;i<8;i++){
		
		    if(data&flag){
				
				   SDA_H;
				
				}
				
				else{   SDA_L;           }
		
				 flag>>=1;
				
		    SCL_H;
				
				SCL_L;
		
		
		
	}
      



}



void IIC_Start(){
	
	  
	   SCL_H;
	   SDA_H;
	   SDA_L;
	   SCL_L;
	
	
	
	
}

void IIC_Stop(){
	
	
	   SCL_H;
	   SDA_L;
	   SDA_H;
	   SCL_L;
	
	
}

void IIC_Ack(){
	
	
    SCL_H;
	  SCL_L;






}



